﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;

namespace Janksen.ValidationHelper
{
	/// <summary>
	/// 字符验证帮助类
	/// </summary>
	public static class StringValidationHelper
	{
		/// <summary>
		/// 验证System.String类型的参数不为空
		/// </summary>
		/// <param name="current">用于验证的ValidationHelpe</param>
		/// <returns>引用本身</returns>
		public static ValidationHelper<string> NotEmpty(this ValidationHelper<string> current)
		{
			return NotEmpty(current , String.Format("{0}不可为空字符串。" , current.ValidationName));
		}

		/// <summary>
		/// 验证System.String类型的参数不为空
		/// </summary>
		/// <param name="current">用于验证的ValidationHelpe</param>
		/// <param name="errorMsg">验证失败提示信息</param>
		/// <returns>引用本身</returns>
		public static ValidationHelper<string> NotEmpty(this ValidationHelper<string> current , string errorMsg)
		{
			if (current.ValidationValue.Length == 0)
			{
				current.FailedValidate(errorMsg);
			}
			return current;
		}

		/// <summary>
		/// 验证System.String类型的参数的长度小于一定值
		/// </summary>
		/// <param name="current">用于验证的ValidationHelpe</param>
		/// <param name="length">最小长度</param>
		/// <returns>引用本身</returns>
		public static ValidationHelper<string> ShorterThan(this ValidationHelper<string> current , int length)
		{
			return ShorterThan(current , length , String.Format("{0}的长度不可超过{1}。" , current.ValidationName , length));
		}

		/// <summary>
		/// 验证System.String类型的参数的长度小于一定值
		/// </summary>
		/// <param name="current">用于验证的ValidationHelpe</param>
		/// <param name="length">最小长度</param>
		/// <param name="errorMsg">验证失败提示信息</param>
		/// <returns>引用本身</returns>
		public static ValidationHelper<string> ShorterThan(this ValidationHelper<string> current , int length , string errorMsg)
		{
			if (current.ValidationValue.Length > length)
			{
				current.FailedValidate(errorMsg);
			}
			return current;
		}

		/// <summary>
		/// 验证System.String类型的参数的长度小于一定值
		/// </summary>
		/// <param name="current">用于验证的ValidationHelpe</param>
		/// <param name="length">最小长度</param>
		/// <returns>引用本身</returns>
		public static ValidationHelper<string> LongerThan(this ValidationHelper<string> current , int length)
		{
			return LongerThan(current , length , String.Format("{0}的长度不可小于{1}。" , current.ValidationName , length));
		}

		/// <summary>	 
		/// 验证System.String类型的参数的长度小于一定值</summary>
		/// <param name="current">用于验证的ValidationHelpe</param>
		/// <param name="length">最小长度</param>
		/// <param name="errorMsg">验证失败提示信息</param>
		/// <returns>引用本身</returns>
		public static ValidationHelper<string> LongerThan(this ValidationHelper<string> current , int length , string errorMsg)
		{
			if (current.ValidationValue.Length < length)
			{
				current.FailedValidate(errorMsg);
			}
			return current;
		}

		/// <summary>
		/// 验证System.String类型的参数的长度在一定的范围内
		/// </summary>
		/// <param name="current">用于验证的ValidationHelpe</param>
		/// <param name="minLength">最小长度</param>
		/// <param name="maxLength">最大长度</param>
		/// <returns>引用本身</returns>
		public static ValidationHelper<string> LengthBetween(this ValidationHelper<string> current , int minLength , int maxLength)
		{
			return LengthBetween(current , minLength , maxLength , String.Format("{0}的长度必须在{1}和{2}之间。" , current.ValidationName , minLength , maxLength));
		}

		/// <summary>
		/// 验证System.String类型的参数的长度在一定的范围内
		/// </summary>
		/// <param name="current">用于验证的ValidationHelpe</param>
		/// <param name="minLength">最小长度</param>
		/// <param name="maxLength">最大长度</param>
		/// <param name="errorMsg">验证失败提示信息</param>
		/// <returns>引用本身</returns>
		public static ValidationHelper<string> LengthBetween(this ValidationHelper<string> current , int minLength , int maxLength , string errorMsg)
		{
			if (current.ValidationValue.Length < minLength || current.ValidationValue.Length > maxLength)
			{
				current.FailedValidate(errorMsg);
			}
			return current;
		}


		/// <summary>
		/// 通过正则表达式验证
		/// </summary>
		/// <param name="current">用于验证的ValidationHelpe</param>
		/// <param name="regexString">正则表达式</param>
		/// <param name="regexOption">验证选项</param>
		/// <param name="errorMsg">不通过验证的提示信息</param>
		/// <returns>引用本身</returns>
		public static ValidationHelper<string> RegexValidate(this ValidationHelper<string> current , string regexString , RegexOptions regexOption , string errorMsg)
		{
			Regex regexTest=new Regex(regexString , regexOption);
			if (!regexTest.IsMatch(current.ValidationValue))
			{
				current.FailedValidate(errorMsg);
			}
			return current;
		}


		/// <summary>
		/// 从配置文件中读取正则表达式验证
		/// </summary>
		/// <param name="current">用于验证的ValidationHelpe</param>
		/// <param name="regexXmlConfigKey">配置文件中的正则表达式Key</param>
		/// <returns></returns>
		public static ValidationHelper<string> RegexValidate(this ValidationHelper<string> current , string regexXmlConfigKey)
		{
			StringValidationConfigHelper.ConfigNode xmlConfigNode=StringValidationConfigHelper.GetXmlConfigNode(regexXmlConfigKey);

			return RegexValidate(current , xmlConfigNode.Value , RegexOptions.None , xmlConfigNode.Message);
		}
	}
}
